﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;

namespace EDM
{
    //public class RoleService : IRoleService
    public partial  class RoleService : IRoleService
    {
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(int Id)
        {
            using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
            {

                Role role = db.Roles.Where(p => p.RoleId == Id).FirstOrDefault();
                if (role != null)
                {
                    db.DeleteObject(role);
                    db.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        public void Update(IList<BLL.Role> roleList)//model
        {
            using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
            {
                foreach (BLL.Role r in roleList)
                {

                    Role r_EF = db.Roles.Where(p => p.RoleId == r.RoleId).FirstOrDefault();
                    r_EF.StatusId = r.StatusText.StatusId;


                }
                db.SaveChanges();

            }

        }
        //public void Update(BLL.Role model)//model
        //{
        //    using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
        //    {
        //         DAL_EF3.Role role = new DAL_EF3.Role();

        //         role = db.Roles.Where(p => p.RoleId == model.RoleId).FirstOrDefault();
        //         if (role.StatusId == (int)RoleStatus.InUse)
        //         {
        //             role.StatusId = (int)RoleStatus.NoUse;
        //             role.RoleName = model.RoleName;
        //             role.RoleId = model.RoleId;
        //         }
        //         if (role.StatusId == (int)RoleStatus.NoUse)
        //         {
        //             role.StatusId = (int)RoleStatus.InUse;
        //             role.RoleName = model.RoleName;
        //             role.RoleId = model.RoleId;
        //         }

        //         BLL.Role roleBLL = new BLL.Role();
        //         roleBLL.RoleId = role.RoleId;
        //         roleBLL.RoleName = role.RoleName;
        //         roleBLL.StatusText.StatusId = role.StatusId;

        //         db.SaveChanges();
        //    }
            
        //}
      

        //public void Update(IList<BLL.Role> roleList)//model
        //{
        //    using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
        //    {
        //        foreach (BLL.Role r in roleList)
        //      {
        //        var roleDE = db.Roles.Where(r => r.RoleId == r.RoleId).FirstOrDefault();
        //            roleDE.RoleId

                  
        /// <summary>
        /// 根据ID返回
        /// </summary>


       
        public  BLL.Role GetById(int Id)////////model
        {
            using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
            {
                Role role = db.Roles.Where(p => p.RoleId == Id).FirstOrDefault();
               
                 BLL.Role roleBLL = new BLL.Role();
                 roleBLL.RoleId = role.RoleId;
                 roleBLL.RoleName = role.RoleName;
                 roleBLL.StatusText.StatusId = role.StatusId;
                 return roleBLL;

            }
        }
        /// <summary>
        /// 返回根据条件
        /// </summary>
        public IList<BLL.Role> GetRole(string roleName, int status)
        {

            using (TrainingWorkshopEntities db = new TrainingWorkshopEntities())
            {
                var listDE = db.Roles.AsQueryable();
                List<BLL.Role> rolelistBE = new List<BLL.Role>();

                if (roleName.Trim().Length > 0)
                    listDE = from p in listDE
                             where p.RoleName.Contains(roleName)
                             select p;
                if (status != 0)
                    listDE = from p in listDE
                             where p.StatusId == status
                             select p;


                foreach (Role roleDE in listDE)
                {
                    BLL.Role roleBE = RoleTranslator.TranslateToRoleBE(roleDE);

                    rolelistBE.Add(roleBE);
                }

                return rolelistBE;

            }
        }



    }
}
